import {createRouter, createWebHistory} from 'vue-router'

// 仅做示例，根据业务修改路径等
const Login = () => import('@/view/login/login.vue')
// 主界面，负责管理Websocket连接状态，保障连接状态不断开
const chatMain = () => import('@/view/chat/main.vue')
const friendList = () => import('@/view/chat/friendList/friendList.vue')
const New = () => import('@/view/chat/new/new.vue')
const MSG = () => import('@/view/chat/msg/msg.vue')

const routes = [
    {
        path: '/',
        redirect: '/login',
    },{
        path: '/login',
        component: Login,
        name: 'Login'
    },
    {
        path: '/chat',
        component: chatMain,
        name: 'chatMain',
        redirect: '/chat/friend/list',
        children: [{
            path: '/chat/friend/list',
            name: 'friendList',
            component: friendList
        },{
            path: '/chat/new',
            name: 'New',
            component: New
        },{
            path: '/chat/msg/:id',
            name: 'msg',
            component: MSG
        }]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 前置路由
router.beforeEach((to, from, next) => {
    const token = window.localStorage.getItem('token')

    if (to.path !== '/login') {
        if (!token) {
            next('/login')
        }
        next()
    } else {
        next()
    }
})

export default router
